package command.project.workspace;

import javax.xml.bind.annotation.XmlTransient;

import model.User;
import model.Workspace;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import controller.ApplicationController;

public class CreateWorkspaceCommand extends AbstractWorkspaceCommand {

	private String userName;
	private Long projectID;
	private Workspace workspace;

	@XmlTransient
	private static final Logger log = LoggerFactory
			.getLogger(CreateWorkspaceCommand.class);

	public CreateWorkspaceCommand(Long projectID, String userName) {
		super();
		this.projectID = projectID;
		this.userName = userName;
	}

	@Override
	public void executeLocal(ApplicationController applicationController) {
		log.debug("Executing command : CreateWorkspaceCommand ...");
		User user = applicationController.getUserByName(userName);
		workspace = new Workspace(user);
		log.debug("Executing command : workspace created, adding to current project");
		applicationController.getModel().getCurrentProject()
				.addWorkspace(workspace);
		log.debug("Executing command : CreateWorkspaceCommand DONE");
	}

	@Override
	public void executeRemote(ApplicationController applicationController) {
		log.debug("Executing remote command : CreateWorkspaceCommand (add workspace to current project)...");
		if (applicationController.getModel().getCurrentProject() != null
				&& applicationController.getModel().getCurrentProject().getId()
						.equals(projectID)) {
			log.debug("Executing remote command : CreateWorkspaceCommand (projet is open) ...");
			applicationController.getModel().getCurrentProject()
					.addWorkspace(workspace);
		} else {
			log.debug("Executing remote command : CreateWorkspaceCommand (projet is not open) ...");
		}
		log.debug("Executing remote command : CreateWorkspaceCommand DONE");
	}

}
